package com.hundsun.s3.platform.staff.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hundsun.s3.common.dto.Staff;
import com.hundsun.s3.common.dto.StaffOrder;
import com.hundsun.s3.platform.monitor.service.impl.SrCount;
import com.hundsun.s3.platform.service.CommonService;
import com.hundsun.s3.staff.service.StaffService;


/**
 * 
 * <p>Title:StaffAction</p>
 * <p>Description:服务人员action类 </p>
 * <p>Company: Hundsun</p>
 * @author :zhangwc23609
 * @date :2018年3月16日 上午9:53:08
 */
@Controller
@RequestMapping("/staff")
public class StaffAction {
	
	@Autowired
	private StaffService staffService;
	
	@Autowired
	private HttpSession session;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private SrCount srCount;
	
	/**
	 * 
	 * @Description:服务人员登录
	 * @param staffId
	 * @param psw
	 * @return status 0 为正常，1为注销
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午2:28:42
	 */
	@RequestMapping(value = "/login.json")
	@ResponseBody
	public Map<String, Object> login(String staffId,String psw){
		Map<String, Object> map = new HashMap<String, Object>();
		//解密
		//psw = BizSecurity.desDecrypt(psw, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
		//加密（此处属于多余，因为前台已经加密了）
		//psw = BizSecurity.desEncrypt(psw, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
		Staff staff = staffService.login(staffId, psw);
		if(staff == null){
			map.put("returnMsg", "用户名或密码错误");
			return map;
		}else if(Integer.parseInt(staff.getStatus()) == 1){
			map.put("returnMsg", "您已经被注销");
			return map;
		}else{
			session.setAttribute(staffId, staff);
			map.put("returnMsg", staff);
			return map;
		}
		
	}
	
	/**
	 * 
	 * @Description:验证服务人员是否已经存在
	 * @param staffId
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午2:32:36
	 */
	@RequestMapping(value = "/judgeStaffId.json")
	@ResponseBody
	public Map<String, Object> judgeStaffId(String staffId){
		Map<String, Object> map = new HashMap<>();
		int result = staffService.judgeStaffId(staffId);
		map.put("isExist", result);
		return map;
	}
	
	/**
	 * 
	 * @Description:服务人员注册
	 * @param staffId
	 * @param psw
	 * @param staffName
	 * @param sex
	 * @param mailBox
	 * @param birthday
	 * @param phoneNumber
	 * @param dept_tree
	 * @param status
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午3:15:34
	 */
	@RequestMapping(value = "/register.json")
	@ResponseBody
	public Map<String, Object> register(String staffId,String psw,String staffName,
			String sex,String mailBox,String birthday,String phoneNumber,
			String dept_tree,String status){
		status = "0";
		Map<String, Object> map = new HashMap<String, Object>();
		//psw = BizSecurity.desEncrypt(psw, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
		dept_tree = commonService.mergeDept(Integer.parseInt(dept_tree));
		String result = staffService.register(staffId, psw, staffName, sex, mailBox, birthday,phoneNumber, dept_tree, status);
		map.put("returnMsg", result);
		return map;
	}
	
	/**
	 * 
	 * @Description:验证邮箱
	 * @param mailBox
	 * @return 1
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午3:20:27
	 */
	@RequestMapping(value = "/judgeMailBox.json")
	@ResponseBody
	public Map<String, Object> judgeMailBox(String mailBox){
		Map<String, Object> map = new HashMap<String, Object>();
		int result = staffService.judgeMailBox(mailBox);
		map.put("isExist", result);
		return map;
	}
	
	/**
	 * 
	 * @Description:退出登录
	 * @param staffId
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午3:58:44
	 */
	@RequestMapping(value = "/logout.json")
	@ResponseBody
	public Map<String, Object> logout(String staffId){
		session.removeAttribute(staffId);
		return null;
	}
	
	/**
	 * 
	 * @Description:服务人员个人主页---个人信息
	 * @param staffId
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月16日 下午4:19:23
	 */
	@RequestMapping(value = "/info.json")
	@ResponseBody
	public Staff getInfoByStaffId(String staffId){
		Staff staff = staffService.getStaffById(staffId);
		return staff;
	}
	
	/**
	 * 
	 * @Description:服务人员个人主页---工单信息
	 * @param staffId
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月19日 上午10:13:55
	 */
	@RequestMapping(value = "/stafforder.json")
	@ResponseBody
	public StaffOrder getOrderInfoByStaffId(String staffId){
		StaffOrder stafforder = staffService.getStaffOrderById(staffId);
		String avgTakingTime = srCount.getAvgTakingTimeById(staffId);
		String avgChatTime = srCount.getAvgChatTimeById(staffId);
		stafforder.setAvgTakingTime(avgTakingTime);
		stafforder.setAvgChatTime(avgChatTime);
		return stafforder;
	}
	
	/**
	 * 
	 * @Description:服务人员修改个人信息
	 * @param staffId
	 * @param staffName
	 * @param sex
	 * @param mailBox
	 * @param birthday
	 * @param phoneNumber
	 * @param deptId
	 * @param status
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月20日 上午10:10:31
	 */
	@RequestMapping(value = "/updatestaff.json")
	@ResponseBody
	public Map<String, Object> modifyStaffInfo(String staffId,String staffName,
			String sex,String mailBox,String birthday,String phoneNumber,
			String deptId,String status){
		Map<String, Object> map = new HashMap<String, Object>();
		deptId = commonService.mergeDept(Integer.parseInt(deptId));
		String result = staffService.modifyStaffInfo(staffId, staffName, sex, mailBox, birthday, phoneNumber, deptId, status);
		map.put("returnMsg", result);
		return map;
		
	}
	
	/**
	 * 
	 * @Description:服务人员修改密码
	 * @param staffId
	 * @param old_psw 旧密码
	 * @param new_psw
	 * @return
	 * @author: zhangwc23609
	 * @date: 2018年3月21日 下午2:15:41
	 */
	@RequestMapping(value = "/updatepsw.json")
	@ResponseBody
	public Map<String, Object> modifyStaffPsw(String staffId,String old_psw,String new_psw){
		Map<String, Object> map = new HashMap<String, Object>();
		Staff staff = staffService.selectPsw(staffId);
		String psw = staff.getPsw();
		if(old_psw == psw){
			staffService.findPwd(staffId, new_psw);
			map.put("returnMsg", "修改密码成功！");
			return map;
		}else{
			map.put("returnMsg", "修改密码失败！");
			return map;
		}
	}
	
}
